<?php
//取得命令
if (empty($app_name = $argv[1])) {
    exit("Usage: appServer.php app_name" . PHP_EOL);
}

//PHP版本
if(version_compare(PHP_VERSION, '5.4.0','<')){
	exit('php version >=5.4.0 require'.PHP_EOL);
}
//swoole版本检查
if (version_compare(SWOOLE_VERSION, '1.7.6', '<')) {
    exit("HttpServer Swoole >= 1.7.6 required " . PHP_EOL);
}

//是否安装了swoole扩展
if(!get_extension_funcs('swoole')){
	exit('HttpServer need swoole extension'.PHP_EOL);
}

//swoole版本检查
if (version_compare(SWOOLE_VERSION, '1.7.6', '<')) {
    exit("HttpServer Swoole >= 1.7.6 required " . PHP_EOL);
}

//需要exec执行命令
if (!function_exists('exec')) {
    exit("HttpServer must enable exec " . PHP_EOL);
}

//读取配置文件
$config_file = __DIR__.'/etc/'.$app_name.'.ini';
if (!is_file($config_file) || false === $config = parse_ini_file($config_file, true)) {
    echo 'the config file no such or no parse' . PHP_EOL;
    return;
}
unset($config_file);

//检查配置
checkConfig($config);

//设置公共常量

define('DOCUMENT_ROOT',rtrim($config['server']['document_root'],'/').'/');
define('TMP_PATH',DOCUMENT_ROOT.'tmp/');
define('APP_NAME',$app_name);
define('APP_PATH',rtrim($config['app']['app_path'],'/').'/');
define('APP_TMP_PATH',APP_PATH.'tmp/');
define('TPL_PATH',APP_PATH.'Tpl/');
define('WIDGET_PATH',DOCUMENT_ROOT.'Widget/');
define('LOG_PATH',APP_PATH.'Log/');
define('SERVER_NAME','CXSV');
// 内存文件路径
define('SHM_PATH','/dev/shm/'.SERVER_NAME.'/'.APP_NAME.'/');
// 创建临时文件
if(!is_dir(TMP_PATH)){
	mkdir(TMP_PATH,0777,true);
}
if(!is_dir(APP_TMP_PATH)){
	mkdir(APP_TMP_PATH,0777,true);
}
//调试模式
if(isset($config['debug']) && !empty($config['debug'])){
	define('DEBUG',1);
    error_reporting(1);
}else{
	error_reporting(0);
}

require __DIR__.'/load.php';
global $php;
$server = new \Lib\AppServer($config);
$server->onRequest(array($server,'start'));
$php = &$server;
$server->run();


function checkConfig(&$config){
	extract($config);
	//检查服务器设置
	extract($server,EXTR_OVERWRITE);
	if(!isset($document_root) || empty($document_root)){
		exit('HttpServer must set the server.document_root');
	}
	if(!isset($host) || empty($host)){
		$config['server']['host'] = '127.0.0.1';
	}
	if(!isset($port) || empty($port)){
		exit('HttpServer must set the server.port');
	}
	// 检查session设置
	extract($session,EXTR_OVERWRITE);
	if(!isset($type) || empty($type)){
		exit('HttpServer must set the session.type');
	}
	if(!isset($session_life) || empty($session_life)){
		exit('HttpServer must set the session.session_life');
	}

	// 检查APP设置
	extract($app,EXTR_OVERWRITE);
	if(!isset($app_path) || empty($app_path)){
		exit('HttpServer must set the app.app_path');
	}

	// 检查CACHE设置
	extract($cache,EXTR_OVERWRITE);
	if(!isset($type) || empty($type)){
		exit('HttpServer must set the cache.type');
	}
	if(!isset($host) || empty($host)){
		exit('HttpServer must set the cache.host');
	}

	// 检查数据库设置
	extract($db,EXTR_OVERWRITE);
	if((!isset($host) || empty($host)) && ((!isset($read) || empty($read)) || (!isset($write) || empty($write)))){
		exit('HttpServer must set the db.host || db.read && db.write link');
	}

	// 检查储存系统设置
	extract($storage,EXTR_OVERWRITE);
	if(!isset($type) || empty($type)){
		exit('HttpServer must set the storage.type');
	}
}